package com.ruoyi.job.task;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.omp.api.RemoteBillService;
import com.ruoyi.omp.api.RemoteFineService;
import com.ruoyi.omp.api.domain.FeBill;
import com.ruoyi.omp.api.domain.FeFine;
import com.ruoyi.omp.api.enums.BillStateEnum;
import com.ruoyi.omp.api.enums.PayStateEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @author: cjie
 * @description: 违约金定时任务
 * @date: 2021/11/9
 */
@Component("fineTask")
public class FineTask {
    private static final Logger logger = LoggerFactory.getLogger(FineTask.class);

    @Autowired
    private RemoteBillService billService;
    @Autowired
    private RemoteFineService remoteFineService;

    /**
     * 生成违约金
     */
    public void createFine() {
        FeBill queryBill = new FeBill();
        //查询账单是执行中并且已过截止期限未支付完成的账单
        queryBill.setBillState(BillStateEnum.执行中.getCode());
        queryBill.setDeadline(DateUtil.date());
        queryBill.setPayStateList(Arrays.asList(PayStateEnum.待支付.getCode(), PayStateEnum.部分支付.getCode()));
        R<List<FeBill>> listR = billService.listBill(queryBill);
        if (listR.getCode() == HttpStatus.SUCCESS) {
            listR.getData().forEach(feBill -> {
                R<FeFine> fineR = remoteFineService.listLastFine(feBill.getId());
                if (fineR.getCode() == HttpStatus.SUCCESS) {
                    //查询账单未缴费金额
                    BigDecimal arrears = feBill.getReceivableAmount().subtract(feBill.getPayAmount());
                    //欠费金额未改变的，更新欠费天数和欠费金额
                    if (fineR.getData() != null && fineR.getData().getArrears().equals(arrears)) {
                        FeFine lastFine = fineR.getData();
                        lastFine.setArrearsDay(lastFine.getArrearsDay() + DateUtil.betweenDay(lastFine.getEndDate(), DateUtil.date(), true));
                        lastFine.setEndDate(DateUtil.date());
                        lastFine.setFine(lastFine.getArrears().multiply(lastFine.getFineRatio()).multiply(BigDecimal.valueOf(lastFine.getArrearsDay())));
                        remoteFineService.updateFine(lastFine);
                    } else {
                        FeFine feFine = new FeFine();
                        feFine.setBillId(feBill.getId());
                        feFine.setBeginDate(DateUtil.date());
                        feFine.setEndDate(DateUtil.date());
                        feFine.setArrears(arrears);
                        feFine.setArrearsDay(DateUtil.betweenDay(feBill.getDeadline(), DateUtil.date(), true));
                        feFine.setFine(feFine.getArrears().multiply(feFine.getFineRatio()).multiply(BigDecimal.valueOf(feFine.getArrearsDay())));
                        remoteFineService.addFine(feFine);
                    }

                }
            });
        }

    }
}
